package com.sskj.gcc.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.gcc.R;
import com.sskj.gcc.component.DaggerUserDataComponent;
import com.sskj.gcc.presenter.SlideMineActivityPresenter;
import com.sskj.gocoin.MyAppLication;
import com.sskj.gocoin.NoLoginDialog;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.Constants;
import com.sskj.gocoin.m.bean.UserInfo;
import com.sskj.gocoin.mine.MyShareActivity;
import com.sskj.gocoin.utils.PhoneHideUtil;
import com.sskj.gocoin.utils.SPUtils;
import com.sskj.gocoin.v.activity.AboutUsActivity;
import com.sskj.gocoin.v.activity.ApplyForShopActivity;
import com.sskj.gocoin.v.activity.CashAddressListActivity;
import com.sskj.gocoin.v.activity.GoogleSettingActivity;
import com.sskj.gocoin.v.activity.PersonalActivity;
import com.sskj.gocoin.v.activity.SecurityCenterActivity;
import com.sskj.gocoin.v.activity.SellerApplyActivity;
import com.sskj.gocoin.v.activity.WebViewActivity;
import com.sskj.gocoin.v.base.BaseActivity;
import com.sskj.lib.RConfig;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.TipUtil;

import javax.inject.Inject;

import butterknife.BindView;
import me.jessyan.autosize.utils.ScreenUtils;

@Route(path = RConfig.SLIDE_MAIN)
public class SlideMineActivity extends BaseActivity<SlideMineActivityPresenter> {

    @BindView(R.id.rightView)
    View rightView;
    @BindView(R.id.llRoot)
    LinearLayout llRoot;
    @BindView(R.id.tvAccount)
    TextView tvAccount;
    @BindView(R.id.tvUid)
    TextView tvUid;
    @BindView(R.id.tvPerson)
    TextView tvPerson;
    @BindView(R.id.tvSecurityCenter)
    TextView tvSecurityCenter;
    @BindView(R.id.tvWithdrawAddress)
    TextView tvWithdrawAddress;
    @BindView(R.id.tvMyPopularize)
    TextView tvMyPopularize;
    @BindView(R.id.tvBusinessCertification)
    TextView tvBusinessCertification;
    @BindView(R.id.tvApplyPmm)
    TextView tvApplyPmm;
    @BindView(R.id.tvAboutUs)
    TextView tvAboutUs;
    @BindView(R.id.tvSetting)
    TextView tvSetting;
    @BindView(R.id.llSign)
    LinearLayout llSign;
    @BindView(R.id.tvSign)
    TextView tvSign;
    @BindView(R.id.tvAssetDetail)
    TextView tvAssetDetail;
    @BindView(R.id.llInfo)
    LinearLayout llInfo;
    private UserInfo mUserInfo;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private AlertDialog dialog;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_slide_mine;
    }

    @Override
    public SlideMineActivityPresenter getPresenter() {
        return new SlideMineActivityPresenter();
    }

    @Override
    public void finish() {
        super.finish();
        //注释掉activity本身的过渡动画
        overridePendingTransition(0, com.sskj.level.R.anim.level_slide_left_out);
    }

    @Override
    protected void initView() {
        overridePendingTransition(com.sskj.level.R.anim.level_slide_left_in, com.sskj.level.R.anim.level_slide_no_move);
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                mPresenter.requestUserInfo();
            } else {
                userData = null;
                updateStatus(null);
            }
        });

        ClickUtil.click(rightView, new ClickUtil.Click() {
            @Override
            public void click() {
                llRoot.setBackgroundColor(Color.TRANSPARENT);
                finish();
            }
        });

        ClickUtil.click(llInfo, () -> {
            if (userData == null) {
                ARouter.getInstance().build(RConfig.APP_LOGIN).navigation();
                overridePendingTransition(R.anim.lib_anim_in, R.anim.lib_anim_out);
                finish();
                return;
            }
            startActivity(new Intent(this, PersonalActivity.class));
            llRoot.setBackgroundColor(Color.TRANSPARENT);
            finish();
        });
        //个人资料
        ClickUtil.click(tvPerson, () -> {
            if (!isLogin()) return;
            startActivity(new Intent(this, PersonalActivity.class));
            llRoot.setBackgroundColor(Color.TRANSPARENT);
            finish();
        });
        //安全中心
        ClickUtil.click(tvSecurityCenter, () -> {
            if (!isLogin()) return;
            startActivity(new Intent(this, SecurityCenterActivity.class));
            llRoot.setBackgroundColor(Color.TRANSPARENT);
            finish();
        });
        //提币地址
        ClickUtil.click(tvWithdrawAddress, () -> {
            if (!isLogin()) return;
            if (!googleBind()) {
                return;
            }
            startActivity(new Intent(this, CashAddressListActivity.class));
            llRoot.setBackgroundColor(Color.TRANSPARENT);
            finish();
        });
        //我的推广
        ClickUtil.click(tvMyPopularize, () -> {
            if (!isLogin()) return;
            startActivity(new Intent(this, MyShareActivity.class));
            llRoot.setBackgroundColor(Color.TRANSPARENT);
            finish();
        });
        //账单明细
        ClickUtil.click(tvAssetDetail, () -> {
            if (!isLogin()) return;
            startActivity(new Intent(this, BillingDetailsActivity.class));
            llRoot.setBackgroundColor(Color.TRANSPARENT);
            finish();
        });
        //商家认证
        ClickUtil.click(tvBusinessCertification, () -> {
            if (!isLogin()) {
                return;
            }
            if (!googleBind()) {
                return;
            }
            if (!checkIdent()) {//是否实名认证
                return;
            }
            if (userData.getConfig().getPayment_password_set()==0) {//是否设置资金密码
                ToastUtil.showShort(App.INSTANCE.getString(com.sskj.gocoin.R.string.strMainActivity218));
                ARouter.getInstance().build(RConfig.MINE_SECURITY).navigation();
                return;
            }

            if (userData.getIs_shop() == 2) {
                ToastUtil.showShort(getString(com.sskj.gocoin.R.string.my_sjshz));
                return;
            }
            if (userData.getIs_shop() == 1) {
                View dialogView = LayoutInflater.from(this).inflate(R.layout.app_dialog_unbound, null);
                dialog = new AlertDialog.Builder(this, R.style.custom_dialog)
                        .setView(dialogView)
                        .show();
                Window window = dialog.getWindow();
                int[] size = ScreenUtils.getScreenSize(this);
                WindowManager.LayoutParams layoutParams = window.getAttributes();
                layoutParams.width = (int) (size[0] * 0.9f);
                window.setAttributes(layoutParams);
                TextView tvApply = dialogView.findViewById(R.id.tv_apply);
                TextView tvCancel = dialogView.findViewById(R.id.tv_cancel);
                ClickUtil.click(tvCancel, () -> dialog.dismiss());
                ClickUtil.click(tvApply, () -> mPresenter.requestRelieveBusiness());
                return;
            }
            if (!checkIdentHign(this)) {
                return;
            }
            SellerApplyActivity.start(this);
        });
        //申请做市商
        ClickUtil.click(tvApplyPmm, new ClickUtil.Click() {
            @Override
            public void click() {
                if (!isLogin()) return;
                WebViewActivity.start(SlideMineActivity.this, "sqss");
                llRoot.setBackgroundColor(Color.TRANSPARENT);
                finish();
            }
        });

        //关于我们
        ClickUtil.click(tvAboutUs, () -> {
            startActivity(new Intent(this, AboutUsActivity.class));
            llRoot.setBackgroundColor(Color.TRANSPARENT);
            finish();
        });
        //设置
        ClickUtil.click(tvSetting, () -> {
            startActivity(new Intent(this, GoogleSettingActivity.class));
            llRoot.setBackgroundColor(Color.TRANSPARENT);
            finish();
        });
        //签到
        ClickUtil.click(llSign, () -> {
            if (!isLogin()) return;
            if (App.INSTANCE.getString(R.string.signed).equals(tvSign.getText().toString().trim())) {
                return;
            }
            mPresenter.signIn();
        });
    }

    private boolean isLogin() {
        if (userData == null) {
            NoLoginDialog noLoginDialog = new NoLoginDialog(this);
            noLoginDialog.show();
            return false;
        }
        return true;
    }

    public void updateStatus(UserInfo data) {
        mUserInfo = data;
        if (data != null) {
            SPUtils.putBean(this, Constants.SP_USER_INFO, data);
            SPUtils.put(this, Constants.SP_REALNAME, data.getName());
            SPUtils.put(MyAppLication.getApplication(), Constants.SP_REALNAME_STATUS, data.getAuthentication());
            tvUid.setVisibility(View.VISIBLE);
            if (data.getQd() == 1) {
                tvSign.setText(App.INSTANCE.getString(com.sskj.gocoin.R.string.signed));
            } else {
                tvSign.setText(App.INSTANCE.getString(com.sskj.gocoin.R.string.sign));
            }
            String s = SPUtil.get(Constants.SP_MOBILE, "");
            if (TextUtils.isEmpty(data.getName())) {
                if (!s.contains("@")) {
                    tvAccount.setText(data == null ? App.INSTANCE.getString(com.sskj.gocoin.R.string.fabimineJavaMineFragment4) : PhoneHideUtil.getHide(s));
                } else {
                    tvAccount.setText(data == null ? App.INSTANCE.getString(com.sskj.gocoin.R.string.fabimineJavaMineFragment4) : PhoneHideUtil.getEmailHide(s));
                }
            } else {
                tvAccount.setText(data.getName());
            }

            tvUid.setText(data == null ? "--" : "UID:" + data.getAccount());
//            if (mUserInfo.isIs_shop()) {
//                menuData.set(5, new Menu(App.INSTANCE.getString(R.string.fabimineJavaMineFragment12), R.mipmap.h_mine_apply_seller, SellerApplyActivity.class));
//                menuAdapter.notifyDataSetChanged();
//            } else {
//                menuData.set(5, new Menu(App.INSTANCE.getString(R.string.strMineFragment210), R.mipmap.h_mine_apply_seller, SellerApplyActivity.class));
//                menuAdapter.notifyDataSetChanged();
//            }
        } else {
            tvAccount.setText(App.INSTANCE.getString(R.string.my_login_rigister));
            tvUid.setText("--");
        }
    }

    public void signInSuccess() {
        tvSign.setText(App.INSTANCE.getString(com.sskj.gocoin.R.string.signed));
    }

    public void onRelieveBusinessSuccess() {
        if (dialog != null) {
            dialog.dismiss();
        }
        mUserInfo.is_shop = false;
        userData.setIs_shop(0);
        userViewModel.update(userData);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (userViewModel != null) {
            userViewModel.update();
        }
    }

    /**
     * 谷歌绑定
     */
    private boolean googleBind() {
        if (userData.getConfig().getGoogle_bind() == 0) {
            com.sskj.lib.util.TipUtil.googleBindTip(this, getString(com.sskj.gocoin.R.string.lib_google_bind), () -> {
                ARouter.getInstance().build(RConfig.MINE_SECURITY).navigation();
            });
            return false;
        } else {
            return true;
        }
    }
}
